package mo.leetcode_top;

import java.util.Arrays;

public class L88 {

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        if (m == 0) {
            for (int i = 0; i < n; i++) {
                nums1[i] = nums2[i];
            }
            return;
        }
        if (n == 0) {
            return;
        }
        int[] part = new int[m];
        System.arraycopy(nums1, 0, part, 0, m);
        int i = 0;
        int j = 0;
        int index = 0;
        while (i < m && j < n) {
            if (part[i] <= nums2[j]) {
                nums1[index++] = part[i++];
            } else {
                nums1[index++] = nums2[j];
                j++;
            }
        }
        while (i < m) {
            nums1[index++] = part[i++];
        }
        while (j < n) {
            nums1[index++] = nums2[j++];
        }
    }

    public static void main(String[] args) {
        L88 test = new L88();
        int[] nums1 = new int[]{0};
        int[] nums2 = new int[]{1};
        test.merge(nums1, 0, nums2, 1);
        System.out.println(Arrays.toString(nums1));
    }

}
